<template>
  <div class="login">
    <div class="loginBox">
      <h2 class="title">OA账号密码登录</h2>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import loginRequest from "../request/index";
import cookie from "js-cookie";
export default {
  data() {
    var username = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("账号不能为空"));
      } else {
        return callback();
      }
    };
    var pass = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("密码不能为空"));
      } else {
        return callback();
      }
    };

    return {
      ruleForm: {
        username: "",
        pass: "",
      },
      rules: {
        username: [{ validator: username, trigger: "blur" }],
        pass: [{ validator: pass, trigger: "blur" }],
      },
    };
  },

  methods: {
    // 登录
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          loginRequest
            .userLogin({
              userName: this.ruleForm.username,
              passWord: this.ruleForm.pass,
            })
            .then((res) => {
              console.log(res);
              if (res.data.success == true) {
                let userinfo = res.data.result;
                // 获取token  存储在本地
                cookie.set("satoken", userinfo.token);
                // 获取用户信息  存储在本地
                window.localStorage.setItem(
                  "userinfo",
                  JSON.stringify(userinfo)
                );
                if (userinfo.type == "user") {
                  //判断用户类型 跳转对应页面
                  this.$notify({
                    title: "欢迎回来，" + this.ruleForm.username,
                    type: "success",
                  });
                  this.$router.push({ path: "/home" });
                } else {
                  //判断用户类型 跳转对应页面
                  this.$notify({
                    title: "欢迎回来，" + this.ruleForm.username,
                    type: "success",
                  });
                  this.$router.push({ path: "/adminhome" });
                }
              } else {
                this.$message.error(res.data.message);
              }
            });
        } else {
          this.$message.error("请检查您的账号密码!");
          return false;
        }
      });
    },
  },

  created() {},
};
</script>

<style lang="scss" scoped>
@import "../css/Login.scss";
</style>